//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;
using OpenLS.Chart.UI.Internals;
using OpenLS.Drawing;

namespace OpenLS.Chart.UI
{
    partial class ValueAxisResources
    {
        public ValueAxisResources()
        {
            InitializeComponent();
        }
        static readonly ValueAxisResources _singleton = new ValueAxisResources();
        public static DataTemplate NumericValueLabelTemplate { get { return (DataTemplate)_singleton["NumericValueLabelTemplate"]; } }
        public static DataTemplate NumericValueTickTemplate { get { return (DataTemplate)_singleton["NumericValueTickTemplate"]; } }
        //  public static Style ValueAxisControlStyle { get { return (Style)_singleton["ValueAxisControlStyle"]; } }
        private void onFormatAxis(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis)e.Parameter;
            PropertyHelper.ShowPropertiesFor(axis, sender as FrameworkElement, ChartCommands.FormatAxis.Text, PropertyTabHint.None);
            e.Handled = true;
        }
        private void onDelete(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis)e.Parameter;
            axis.Delete = true;
            e.Handled = true;
        }
        private void onAddMajorGridLines(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis) e.Parameter;
            axis.MajorGridLines = new ChartLines();
            e.Handled = true;
        }
        private void onAddMinorGridLines(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis) e.Parameter;
            axis.MinorGridLines = new ChartLines();
            e.Handled = true;
        }
        private void onFormatMajorGridLines(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis) e.Parameter;
            if (axis.MajorGridLines == null)
                return;
            PropertyHelper.ShowPropertiesFor(axis.MajorGridLines, sender as FrameworkElement, ChartCommands.FormatMajorGridLines.Text, PropertyTabHint.None);
            e.Handled = true;
        }
        private void onFormatMinorGridLines(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis) e.Parameter;
            if (axis.MinorGridLines == null)
                return;
            PropertyHelper.ShowPropertiesFor(axis.MinorGridLines, sender as FrameworkElement, ChartCommands.FormatMinorGridLines.Text, PropertyTabHint.None);
            e.Handled = true;
        }

        private void onCanAddMajorGridLines(object sender, CanExecuteRoutedEventArgs e)
        {
            onCanExecute(e, axis => axis.MajorGridLines  == null);

        }
        private void onCanAddMinorGridLines(object sender, CanExecuteRoutedEventArgs e)
        {
            onCanExecute(e, axis => axis.MinorGridLines == null && axis.MajorGridLines != null);
        }

        private void onCanFormatMajorGridLines(object sender, CanExecuteRoutedEventArgs e)
        {
            onCanExecute(e, axis => axis.MajorGridLines != null);
        }
        private void onCanFormatMinorGridLines(object sender, CanExecuteRoutedEventArgs e)
        {
            onCanExecute(e, axis => axis.MinorGridLines != null);
        }

        static void onCanExecute (CanExecuteRoutedEventArgs e, Func<Axis, bool> fn)
        {
            var axis = (Axis) e.Parameter;
            e.CanExecute = fn(axis);
            e.Handled = true;
        }
        void onMouseDown(object sender, MouseButtonEventArgs args)
        {
            ((IInputElement)sender).Focus();
            args.Handled = true;
        }


        private void onAddTitle(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = (Axis)e.Parameter;
            axis.Title = new Title(new TextBody(UIStringTable.DefaultTitle)); 
            e.Handled = true;
        }

        private void onCanAddTitle(object sender, CanExecuteRoutedEventArgs e)
        {
            onCanExecute(e, axis => axis != null &&  axis.Title == null);
        }

        private void onDeleteTitle(object sender, ExecutedRoutedEventArgs e)
        {
            var axis = AxisElement.GetAxis((DependencyObject) sender);
            axis.Title = null;
            e.Handled = true;
        }
    }

    delegate T2 Func <T1, T2>(T1 t1);
}
